var app = getApp()
let orderStatus = 0;
let db = wx.cloud.database();
Page({
  data: {
    navbar: ["待接单", "已接单", "已完成"],
    currentTab: 0,
    list: []
  },
  //顶部tab切换
  navbarTap: function (e) {
    let index = e.currentTarget.dataset.idx;
    this.setData({
      currentTab: index
    })
    if (index == 0) {
      orderStatus = 0;
    } else if (index == 1) {
      orderStatus = 1;
    } else if (index == 2) {
      orderStatus = 2;
    } else {
      orderStatus = 0;
    }
    this.getMyOrderList();
  },

  onShow: function () {
    this.getMyOrderList();
  },

  getMyOrderList() {
    wx.cloud.callFunction({
        name: 'order',
        data: {
          action: 'myorder',
          status: orderStatus
        }
      })
      .then(res => {
        console.log("我的发布列表", res)
        this.setData({
          list: res.result.data
        })
      }).catch(res => {
        console.log("我的发布列表失败", res)
      })
  },
  close() {
    this.setData({
        is_show: false,
    })
},
open(e) {
console.log(e)
    this.setData({
        is_show: true,
        url: e.currentTarget.dataset.img
    })
},

  //确认收货
  confirm(event) {
    var item = event.currentTarget.dataset.item;
    wx.cloud.callFunction({
        name: 'order',
        data: {
          action: 'confirm',
          _id: item._id
        }
      })
      .then(res => {
        console.log("确认完成", res)
        if (res.result.stats && res.result.stats.updated > 0) {
          this.getMyOrderList()
          wx.showToast({
            title: '收货成功'
          })
         this.jdraddMoney(item.jiedanid,item.money)
        } else {
          wx.showToast({
            icon: 'error',
            title: '收货失败',
          })
        }
      }).catch(res => {
        console.log("确认完成失败", res)
        wx.showToast({
          icon: 'none',
          title: '收货失败'
        })
      })
  },
  makePhoneCall(e){
    console.log(e)
      wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.phonenumber,
      })
  },
  jdraddMoney(openid,money) {
    var money = money
    var openid = openid
    let db = wx.cloud.database()
    let _ = db.command
    db.collection('students').doc(openid)
      .update({
        data: {
          money: _.inc(money)
        }
      }).then(res => {
        console.log('增加金额成功', res)
        if (res.stats && res.stats.updated > 0) {
          wx.cloud.database().collection('jilu').add({
            data: {
              time: this._getCurrentTime(),
              money: '+'+ money,
              openid: openid
            }
          })
        }
      })
  },
  _getCurrentTime(date) {
    var d = new Date();
    if (date) {
      var d = new Date(date);
    }
    var month = d.getMonth() + 1;
    var date = d.getDate();
    var day = d.getDay();
    var hours = d.getHours();
    var minutes = d.getMinutes();
    var curDateTime = d.getFullYear() + '年';
    if (month > 9)
      curDateTime += month + '月';
    else
      curDateTime += month + '月';

    if (date > 9)
      curDateTime = curDateTime + date + "日";
    else
      curDateTime = curDateTime + date + "日";
    if (hours > 9)
      curDateTime = curDateTime + hours + "时";
    else
      curDateTime = curDateTime + hours + "时";
    if (minutes > 9)
      curDateTime = curDateTime + minutes + "分";
    else
      curDateTime = curDateTime + minutes + "分";
    return curDateTime;
  },
})